<?php
// List all events that the current user took part in that also has a comment from the coach.
// Show 'view comment' link per event which shows the corresponding comment when clicked on.
function showSwimmerCommentView() {
	echo "<div class='featured-post'>
            <div class='post-title'>
                <h2>Coach Comments</h2>
			</div>";
	
	// Get the page number so we know which page to display.
	$pageNum = $_GET['pagenum'];
	if (!(isset($pageNum))) { 
		$pageNum = 1; 
	}
	
	$myID = $_SESSION['ls_userid'];
	// If user is a parent then get comments of their child
	// otherwise get comments of the current swimmer.
	if($_SESSION["ls_accounttype"] == "parent") {
		$query = "SELECT `childID` FROM `parentChild`
				WHERE `parentID` = '$myID'";
		$result = mysql_query($query) or die("Could not get childs comments: " . mysql_error());
		$kidsIDs = array();
		while($rows = mysql_fetch_assoc($result)) {
			array_push($kidsIDs, $rows["childID"]);
		}
		$query = "SELECT `ls_comments`.`swimmerID` AS swimmerID
						FROM `ls_comments`
						WHERE swimmerID = '" . $kidsIDs[0] . "'";
		$num = count($kidsIDs);
		for($i = 1; $i < $num; $i++) {
			$query .= " OR swimmerID = '" . $kidsIDs[$i] . "'";
		}
	}
	if($_SESSION["ls_accounttype"] == "swimmer") {
		$query = "SELECT `ls_comments`.`swimmerID` AS swimmerID
						FROM `ls_comments`
						WHERE swimmerID = '$myID'";
	}
	
	$list = mysql_query($query) or die("Could not get users comments: " . mysql_error());
	
	// Get the total number of comments
	$totalRows = mysql_num_rows($list);
	
	// Show comments only if there are comments for the current user.
	if($totalRows == 0) {
		echo "<p>There are no comments from your coaches.</p></div>";
	}
	else {
		// Number of results to show per page.
		$rowLimit = 20;
		
		// Number for last page.
		$last = ceil($totalRows / $rowLimit);
		
		// Make sure page number is in range.
		if($pageNum < 1) {
			$pageNum = 1;
		}
		else if ($pageNum > $last) {
			$pageNum = $last;
		}
		
		// Set the limit range for the sql query.
		if($pageNum > 0) {
			$max = 'LIMIT ' .($pageNum - 1) * $rowLimit . ',' . $rowLimit;
		}
		
		// Run query again with the limit to get the correct rows for the current page.
		// The results will be ordered by the event date starting from the most recent.
		if($_SESSION["ls_accounttype"] == "swimmer") {
			$query = "SELECT `ls_comments`.`commentID` AS commentID, `ls_events`.`eventID`,
						`ls_events`.`name` AS eventName, `ls_events`.`eventType`, 
						`ls_events`.`eventDate`
						FROM `ls_comments`, `ls_events`
						WHERE `ls_comments`.`eventID` = `ls_events`.`eventID`
						AND `ls_comments`.`swimmerID` = '$myID'
						ORDER BY `ls_events`.`eventDate` DESC $max";
		}
		if($_SESSION["ls_accounttype"] == "parent") {
			$query = "SELECT `ls_events`.`eventID` AS eventID, `accounts`.`name` AS kidName, 
						`ls_events`.`name` AS eventName, `ls_events`.`eventType` AS eventType,
						`ls_events`.`eventDate` AS eventDate, `ls_comments`.`coachID` AS coachID,
						`ls_comments`.`commentID` AS commentID
						FROM `ls_comments`, `accounts`, `ls_events`
						WHERE `accounts`.`userID` = `ls_comments`.`swimmerID`
						AND `ls_comments`.`eventID` = `ls_events`.`eventID`
						AND (`ls_comments`.`swimmerID` = '" . $kidsIDs[0] . "'";
			$num = count($kidsIDs);
			for($i = 1; $i < $num; $i++) {
				$query .= " OR `ls_comments`.`swimmerID` = '" . $kidsIDs[$i] . "'";
			}
			$query .= ") AND (`accounts`.`userID` = '" . $kidsIDs[0] . "'";
			for($i = 1; $i < $num; $i++) {
				$query .= " OR `accounts`.`userID` = '" . $kidsIDs[$i] . "'";
			}
			$query .= ") ORDER BY `ls_events`.`eventDate` DESC $max";
		}
		
									
		$list = mysql_query($query) or die("Could not select at showCommentList (2nd). " . mysql_error());
		$kidTitle = ($_SESSION["ls_accounttype"] == "parent") ? "<th>Child</th>" : "";
		// Print out the list of events in an html table.
		echo "<div id='commentBox' class='white_content'><a class='closeBoxLink' href='#' onclick = 'closeBox()'>Close</a>
				<div id='commentText'></div></div>
				<div class='post-entry'>
				<table id='blueTable'><tr>$kidTitle<th>Event Name</th><th>Event Type</th><th>Event Date</th><th>Comments</th></tr>";
		$i = 0;
		while($rows = mysql_fetch_assoc($list)) {
			// Use the $style variable to alternate the css class.
			$style = (($i % 2) == 0)? 'even' : 'odd';
			echo "<tr class='$style'>";
			if($_SESSION["ls_accounttype"] == "parent") {
				echo "<td>" . $rows['kidName'] . "</td>";
			}
			echo "<td>" . $rows['eventName'] . "</td><td>" . $rows['eventType'] . "</td><td>" . $rows['eventDate'] . 
				 "</td><td><a href='javascript:getComment(" . $rows['commentID'] . ")'>View Comments</a></td></tr>";
			$i++;
		}
		echo "</table></div>";
		
		// Do pagination if there are more than 1 pages of results to show.
		if($last > 1) {
			paginate($pageNum, $last);
		}
		
		echo "</div><div id='blackOverlay' class='blackOverlay'></div>";
	}
}
?>